package com.niit.view;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.niit.dao.UsersDao;

public class UsersView {
	

	UsersDao dao = new UsersDao();

	public void welcome() throws SQLException {
		while (true) {
			System.out.println("*************************欢迎来到学生管理系统*************************");
			System.out.println("请选择你要进行的操作：");
			System.out.println("1.注册");
			System.out.println("2.登录");
			Scanner scanner = new Scanner(System.in);
			String nextLine = scanner.nextLine();
			switch (nextLine) {
			case "1":
				register(scanner);
				break;
			case "2":
				login(scanner);
				break;

			default:
				System.out.println("输入错误请重新输入");
				break;
			}
		}
	}

	public void register(Scanner scanner) throws SQLException {
		System.out.println("*************************注册*************************");
		System.out.println("请输入用户名：");
		scanner = new Scanner(System.in);
		String username = scanner.nextLine();
		System.out.println("请输入密码：");
		String password = scanner.nextLine();
		boolean register = dao.register(username, password);
		System.out.println(register ? "注册成功！" : "注册失败！");
	}
	public void login(Scanner scanner) throws SQLException {
		System.out.println("*************************登录*************************");
		System.out.println("请输入用户名：");
		scanner = new Scanner(System.in);
		String username = scanner.nextLine();
		System.out.println("请输入密码：");
		String password = scanner.nextLine();
		int userId = dao.login(username, password);
		System.out.println(userId !=0 ? "登录成功！" : "登录失败！");
		List<Integer> roleByUserId = dao.getRoleByUserId(userId);
		new MeneView().showMainMenu(scanner, roleByUserId, username);
	}

}
